package hng.controller;


import hng.dto.AddressDTO;
import hng.model.AddressDO;
import hng.service.AddressService;
import hng.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 告白
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired(required = false)
    private AddressService addressService;

    /**
     * 添加收货地址
     */
    @PostMapping("/add")
    public JsonData add(@RequestBody AddressDTO addressDTO) {
        addressService.add(addressDTO);
        return JsonData.buildSuccess();
    }

    /**
     * 删除收货地址
     */
    @GetMapping("/del")
    public JsonData del(@RequestParam("id") Integer id) {
        addressService.del(id);
        return JsonData.buildSuccess();
    }

    /**
     * 修改收货地址
     */
    @PostMapping("/update")
    public JsonData update(@RequestBody AddressDTO addressDTO) {
        addressService.update(addressDTO);
        return JsonData.buildSuccess();
    }

    /**
     * 查询收货地址列表
     */
    @GetMapping("/list")
    public JsonData list() {
        List<AddressDO> addressDOList = addressService.list();
        return JsonData.buildSuccess(addressDOList);
    }
}

